 /*
 * Created by SharpDevelop.
 * User: Propriétaire
 * Date: 14/07/2012
 * Time: 09:51
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Windows.Forms;

namespace IPD_GeneralLib
{
    /// <summary>
    /// Description of MainForm.
    /// </summary>
public partial class MainForm : Form
 {
    
    internal static string ARGVFNAME = "" ;
    internal static string ARGVDIR = "" ;
	static string LASTWORDSEARCHED = "" ;
    static int LASTROWSEARCH = -1 ;
    static int LASTCOLSEARCH = -1 ;
    bool COMPUTEselect = false ;

	 
    
public MainForm()
  {
            //
            // The InitializeComponent() call is required for Windows Forms designer support.
            //
            InitializeComponent();
            
            //
            // TODO: Add constructor code after the InitializeComponent() call.
            //
        }
        
void MainFormLoad(object sender, EventArgs e)
 {
 #if TRACE
//ARGVFNAME = @"C:\Documents and Settings\Propriétaire\Bureau\Téléphone\Extractions_Blackberry_Esther\Backup-(2012-09-04) esther.ipd";
//ARGVFNAME = @"D:\Develop_Projects\SharpDevelop Projects\BbIpdManaging-V0.99\BbIpdManaging\bin\Debug\Backup-(2010-03-14)-SMS_ONLY.ipd";
//ARGVFNAME = @"D:\Develop_Projects\SharpDevelop Projects\BbIpdManaging-V0.99\BbIpdManaging\bin\Debug\Onedb_SMS.ipd";
ARGVFNAME = @"D:\Develop_Projects\SharpDevelop Projects\BbIpdManaging-V0.99\BbIpdManaging\bin\Debug\Copie de Backup-(2010-03-14)-SMS_ONLY.ipd";
//ARGVFNAME = @"C:\Documents and Settings\Propriétaire\Bureau\Téléphone\Extractions_Blackberry_8520\Backup-(2012-08-02) 1er jour de la semaine lundi.ipd" ;
//ARGVFNAME = @"C:\Documents and Settings\Propriétaire\Bureau\Téléphone\Extractions_Blackberry Tel Esther 8120\Esther-Backup-(2012-08-07).ipd";
//ARGVFNAME = @"C:\\Documents and Settings\\Propriétaire\\:"; // Invalid file name. Tests purpose
//ARGVFNAME = @"C:\Documents and Settings\Propriétaire\Bureau\Téléphone\Extractions_Blackberry_8520\Backup-(2012-08-02) 1er jour de la semaine dimanche.ipd";
#endif

    Version assemblyVersion = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;
    string sBuild_version = assemblyVersion.Major + "." + assemblyVersion.Minor +
	                        "." + assemblyVersion.Build ;
    this.Text = this.Text + "    v" + sBuild_version ;
                   
	init_colnames_forContacts() ;
	init_colnames_forCalendar() ;
	init_colnames_forSms() ;
	init_colnames_forMemos() ;
	init_colnames_forTasks() ;
	init_colnames_forPhonecallLog() ;
	//init_colnames_forMessages() ;
	
	this.Update() ;
	this.Refresh() ;

	if (ARGVFNAME != "")
       Parse_new_IpdFile() ;
}

void Parse_new_IpdFile()
 {
    Clean_all_formobjects();
    textBox1.Text = ARGVFNAME;
    
	int iret = IPDGeneral.Start_IPD_Parse(ARGVFNAME) ;
	
	if (iret < 0) // IPD not found ?
	   return ;
	
	display_listof_db_in_ipd();
	
	label4.Text = IPD_GeneralLib.IPDGeneral.lSizeOfInputFile.ToString("# ### ###") + " Bytes" ;
	label6.Text = IPD_GeneralLib.IPDGeneral.sIpd_Last_ModifiedDate ;
	label9.Text = listBox1.Items.Count.ToString("####") ;
	displaymemos_inlistv() ;
	displayphonecalllogs_inlistv() ;
	displaysms_inlistv();
	displaytasks_inlistv();
	displaycalendar_inlistv();
	displaycontacts_inlistv() ;
	//displaymessages_inlistv();
	listView1.Focus() ;
}

void displaycontacts_inlistv()
 {
  listView5.Items.Clear() ;
  int inb_enreg = (int) IPD_GeneralLib.IPDGeneral.iContacts_Row ;
  int imaxcol = IPD_GeneralLib.IPDGeneral.VirtualArray_Contacts.GetLength(1) ;
  int irecnumber = 0 ;
  
  for (int row = 0 ; row < inb_enreg ; row++)
      {
        irecnumber = row + 1 ;
        listView5.Items.Add(irecnumber.ToString("0000"));
        for (int col = 1 ; col < imaxcol-1 ; col++) // Don't display picture colonne
            {
              listView5.Items[row].SubItems.Add(IPD_GeneralLib.IPDGeneral.VirtualArray_Contacts[row, col]);
          }
     }
  TabControl1.TabPages[4].Text = "Contacts" + " (" + listView5.Items.Count + ")" ;
}

void displaymemos_inlistv()
 {
  listView1.Items.Clear() ;
  int inb_enreg = (int) IPD_GeneralLib.IPDGeneral.iMemos_Row ;
  int imaxcol = IPD_GeneralLib.IPDGeneral.VirtualArray_Memos.GetLength(1) ;
  int irecnumber = 0 ;
  
  for (int row = 0 ; row < inb_enreg ; row++)
      {
        irecnumber = row + 1 ;
        listView1.Items.Add(irecnumber.ToString("0000"));
        for (int col = 1 ; col < imaxcol ; col++)
             listView1.Items[row].SubItems.Add(IPD_GeneralLib.IPDGeneral.VirtualArray_Memos[row, col]);
     }
  TabControl1.TabPages[0].Text = "Memos" + " (" + listView1.Items.Count + ")" ;
}

void displaycalendar_inlistv()
 {
/*
  CALENDAR_ColSubject = 1;
  CALENDAR_ColNotes = 2;
  CALENDAR_ColDateStart = 3;
  CALENDAR_ColDateEnd = 4;
  CALENDAR_ColRecurrence_Type = 5;
  CALENDAR_ColRecurrence_Start = 6;
  CALENDAR_ColRecurrence_End = 7;
  CALENDAR_ColEvery = 8;
  CALENDAR_ColDateAlarm = 9;
  CALENDAR_ColLocation = 10;
*/
  listView4.Items.Clear() ;
  int inb_enreg = (int) IPD_GeneralLib.IPDGeneral.iCalendar_Row ;
  int imaxcol = IPD_GeneralLib.IPDGeneral.VirtualArray_Calendar.GetLength(1) ;
  int irecnumber = 0 ;
  
  for (int row = 0 ; row < inb_enreg ; row++)
      {
        irecnumber = row + 1 ;
        listView4.Items.Add(irecnumber.ToString("0000"));
        for (int col = 1 ; col < imaxcol ; col++)
            {
              listView4.Items[row].SubItems.Add(IPD_GeneralLib.IPDGeneral.VirtualArray_Calendar[row, col]);
          }
     }
  TabControl1.TabPages[3].Text = "Calendar" + " (" + listView4.Items.Count + ")" ;
}

void displayphonecalllogs_inlistv()
 {
  listView2.Items.Clear() ;
  int inb_enreg = (int) IPD_GeneralLib.IPDGeneral.iPhoneCallLogs_Row ;
  int imaxcol = IPD_GeneralLib.IPDGeneral.VirtualArray_Phone_Call_Log.GetLength(1) ;
  int irecnumber = 0 ;
  
  for (int row = 0 ; row < inb_enreg ; row++)
      {
        irecnumber = row + 1 ;
        listView2.Items.Add(irecnumber.ToString("0000"));
        for (int col = 1 ; col < imaxcol ; col++)
            {
              listView2.Items[row].SubItems.Add(IPD_GeneralLib.IPDGeneral.VirtualArray_Phone_Call_Log[row, col]);
          }
     }
  TabControl1.TabPages[1].Text = "Phone CallLog" + " (" + listView2.Items.Count + ")" ;
}

void displaysms_inlistv()
 {
/* HELP FOR COLUMNS
  SMS_ColDateSent = 1
  SMS_ColDateReceived = 2
  SMS_ColStatus = 3;
  SMS_ColFolder = 4
  SMS_ColPhone = 5
  SMS_ColMsg = 6
*/
  listView6.Items.Clear() ;
  int inb_enreg = (int) IPD_GeneralLib.IPDGeneral.iSMS_Row ;
  int imaxcol = IPD_GeneralLib.IPDGeneral.VirtualArray_SMS.GetLength(1) ;
  int irecnumber = 0 ;
  string scontentlong = "";
  
  for (int irow = 0 ; irow < inb_enreg ; irow++)
      {
        irecnumber = irow + 1 ;
        listView6.Items.Add(irecnumber.ToString("0000"));
        for (int icol = 1 ; icol < imaxcol ; icol++)
            {
              scontentlong = IPD_GeneralLib.IPDGeneral.VirtualArray_SMS[irow, icol];
              if (icol == IPD_GeneralLib.IPDGeneral.SMS_ColStatus)
                  if (scontentlong == "1")
                      scontentlong = "From (Received)";
                         else scontentlong = "To (Sent)";
                         
                         
              if (icol == IPD_GeneralLib.IPDGeneral.SMS_ColFolder)
                  if (scontentlong == "1")
                      scontentlong = "Inbox";
                         else scontentlong = "Outbox";
                     
              listView6.Items[irow].SubItems.Add(scontentlong);
          }
     }
     
  TabControl1.TabPages[5].Text = "Sms" + " (" + listView6.Items.Count + ")" ;
}

/*
void displaymessages_inlistv()
{
  listView7.Items.Clear() ;
  int inb_enreg = (int) IPD_GeneralLib.IPDGeneral.iMessages_Row ;
  int imaxcol = IPD_GeneralLib.IPDGeneral.VirtualArray_Messages.GetLength(1) ;
  int irecnumber = 0 ;
  
  for (int row = 0 ; row < inb_enreg ; row++)
      {
        irecnumber = row + 1 ;
        listView7.Items.Add(irecnumber.ToString("0000"));
        for (int col = 1 ; col < imaxcol ; col++)
            {
              listView7.Items[row].SubItems.Add(IPD_GeneralLib.IPDGeneral.VirtualArray_Messages[row, col]);
          }
     }
  TabControl1.TabPages[5].Text = "Sms" + " (" + listView7.Items.Count + ")" ;
}
*/

void displaytasks_inlistv()
 {
  /*
   TASKS_ColName = 1;            // Name (or subject)
   TASKS_ColNotes = 2;           // Note (description...)
   TASKS_ColDate = 3;            // Beginning date of Task
   TASKS_ColRecurrence_Type = 4; // expl.: dayly, monthly, etc...
   TASKS_ColDateAlarm = 5;        // Duration of reminder
   TASKS_ColEnd = 6;             // End date of Task
   TASKS_ColEvery = 7;           // Repeat every "expl: 1st week each month, ...)
  */
  
  listView3.Items.Clear() ;
  int inb_enreg = (int) IPD_GeneralLib.IPDGeneral.iTasks_Row ;
  int imaxcol = IPD_GeneralLib.IPDGeneral.VirtualArray_Tasks.GetLength(1) ;
  int irecnumber = 0 ;
  
  for (int row = 0 ; row < inb_enreg ; row++)
      {
        irecnumber = row + 1 ;
        listView3.Items.Add(irecnumber.ToString("0000"));
        for (int col = 1 ; col < imaxcol ; col++)
            {
              listView3.Items[row].SubItems.Add(IPD_GeneralLib.IPDGeneral.VirtualArray_Tasks[row, col]);
          }
     }
  TabControl1.TabPages[2].Text = "Tasks" + " (" + listView3.Items.Count + ")" ;
}

void button1Click(object sender, EventArgs e)
 {
   Close() ;
}

// export contacts to VCard
void Button3Click(object sender, EventArgs e)
 {
    int idbid = 0 ;
    int iret ;
    
    label8.Visible = false ; // Done ...
    
    idbid = IPD_GeneralLib.IPDGeneral.DBAbsoluteIdentifier.absolute_idContacts ;
    
    iret = IPD_GeneralLib.IPDGeneral.Dump_Contacts_toVCard(idbid) ; // if error (iret < 0) have to do Something

    label8.Visible = true ; // Done ...
}

// Export Calendar to VCal		
void Button4Click(object sender, EventArgs e)
 {
    int idbid = 0 ;
    int iret ;
    
    label8.Visible = false ; // Done ...
    
    idbid = IPD_GeneralLib.IPDGeneral.DBAbsoluteIdentifier.absolute_idCalendar ;
    
    iret = IPD_GeneralLib.IPDGeneral.Dump_Calendar_toVCal(idbid) ; // if error (iret < 0) have to do Something
    
    label8.Visible = true ; // Done ...
}
		
// Export all record(s) to Csv
void Button5Click(object sender, EventArgs e)
 { 
    int idbid = 0 ;
    int iret ;
    
    label8.Visible = false ; // Done ...
    
    switch (TabControl1.SelectedIndex) {
        case 0:
            idbid = IPD_GeneralLib.IPDGeneral.DBAbsoluteIdentifier.absolute_idMemos ;
            break;
        case 1:
            idbid = IPD_GeneralLib.IPDGeneral.DBAbsoluteIdentifier.absolute_idPhone_Call_Log ;
            break;
        case 2:
            idbid = IPD_GeneralLib.IPDGeneral.DBAbsoluteIdentifier.absolute_idTasks ;
            break;
        case 3:
            idbid = IPD_GeneralLib.IPDGeneral.DBAbsoluteIdentifier.absolute_idCalendar ;
            break;
        case 4:
            idbid = IPD_GeneralLib.IPDGeneral.DBAbsoluteIdentifier.absolute_idContacts ;
            break;
        case 5:
            idbid = IPD_GeneralLib.IPDGeneral.DBAbsoluteIdentifier.absolute_idSMS ;
            break;
        default:
            // Unknown, nothing to do
        	break;
    }
    

    iret = IPD_GeneralLib.IPDGeneral.Dump_Db_toCsv(idbid) ; // if error (iret < 0) have to do Something
    
    label8.Visible = true ; // Done ...
}

void ListView1DoubleClick(object sender, EventArgs e)
 {
  label8.Visible = false ; // Done ...
  Affiche_enregMemos.AFFENRindexenreg = listView1.FocusedItem.Index ;
  Affiche_enregMemos.AFFENRmaxnumenreg = listView1.Items.Count ;

  Affiche_enregMemos mafenetre1 = new Affiche_enregMemos() ; // pour pouvoir ouvrir une form !!!!
  mafenetre1.ShowDialog() ;
}
        
void ListView2DoubleClick(object sender, EventArgs e)
 {
  label8.Visible = false ; // Done ...
  Affiche_enregPhoneCallLog.AFFENRindexenreg = listView2.FocusedItem.Index ;
  Affiche_enregPhoneCallLog.AFFENRmaxnumenreg = listView2.Items.Count ;

  Affiche_enregPhoneCallLog mafenetre1 = new Affiche_enregPhoneCallLog() ; // pour pouvoir ouvrir une form !!!!
  mafenetre1.ShowDialog() ;
}
        
void ListView3DoubleClick(object sender, EventArgs e)
 {
  label8.Visible = false ; // Done ...
  Affiche_enregTasks.AFFENRindexenreg = listView3.FocusedItem.Index ;
  Affiche_enregTasks.AFFENRmaxnumenreg = listView3.Items.Count ;

  Affiche_enregTasks mafenetre1 = new Affiche_enregTasks() ; // pour pouvoir ouvrir une form !!!!
  mafenetre1.ShowDialog() ;
}
        
void ListView6DoubleClick(object sender, EventArgs e)
 {
  label8.Visible = false ; // Done ...
  Affiche_enregSms.AFFENRindexenreg = listView6.FocusedItem.Index ;
  Affiche_enregSms.AFFENRmaxnumenreg = listView6.Items.Count ;

  Affiche_enregSms mafenetre1 = new Affiche_enregSms() ; // pour pouvoir ouvrir une form !!!!
  mafenetre1.ShowDialog() ;
}
        
void ListView4DoubleClick(object sender, EventArgs e)
 {
  label8.Visible = false ; // Done ...
  Affiche_enregCalendar.AFFENRindexenreg = listView4.FocusedItem.Index ;
  Affiche_enregCalendar.AFFENRmaxnumenreg = listView4.Items.Count ;

  Affiche_enregCalendar mafenetre1 = new Affiche_enregCalendar() ; // pour pouvoir ouvrir une form !!!!
  mafenetre1.ShowDialog() ;
}
        
void ListView5DoubleClick(object sender, EventArgs e)
 {
  label8.Visible = false ; // Done ...
  Affiche_enregContacts.AFFENRindexenreg = listView5.FocusedItem.Index ;
  Affiche_enregContacts.AFFENRmaxnumenreg = listView5.Items.Count ;

  Affiche_enregContacts mafenetre1 = new Affiche_enregContacts() ; // pour pouvoir ouvrir une form !!!!
  mafenetre1.ShowDialog() ;
}

void display_listof_db_in_ipd()
 {
   int inbdb = IPD_GeneralLib.IPDGeneral.iNumber_of_DBs ;
   int ii = 0 ;
    
   for (ii = 0 ; ii < inbdb ; ii++)
   {
       string stodisplay = IPD_GeneralLib.IPDGeneral.DB_Info.db_names[ii];
       if (IPD_GeneralLib.IPDGeneral.DB_Info.db_Ok_toprocess[ii] == false)
          stodisplay = "   " + stodisplay;
       listBox1.Items.Add(stodisplay) ;
   }
}

int searchwordinlistview(ListView lv, string lemot)
 {
   int larow = -1 ;
   int lacol = -1 ;
   int inbrow = lv.Items.Count ;
   int inbcols = lv.Columns.Count ;
   bool bexitfirstfor = false ;
   if (LASTWORDSEARCHED != lemot) // nouveau mot(s) pour recherche
       {
        LASTWORDSEARCHED = lemot ;
        LASTROWSEARCH = -1 ;
        LASTCOLSEARCH = -1 ;
      }
   
    int ii = LASTROWSEARCH + 1 ;
    int jj = LASTCOLSEARCH + 1 ;
                   
    for ( ; ii < inbrow ; ii++)
    {
        if (bexitfirstfor == true)
            break ;
        for (jj = 0 ; jj < inbcols ; jj++)
            {
             if (lv.Items[ii].SubItems[jj].Text.ToLower().Contains(LASTWORDSEARCHED.ToLower()) == true)
                {
                  larow = ii ;
                  lacol = jj ;
                  bexitfirstfor = true ;
                  break ;
              }
         }
    }
      
  LASTROWSEARCH = larow ; // si pas trouvé (-1) la prochaine fois on cherche depuis le début
  LASTCOLSEARCH = lacol ; // si pas trouvé (-1) la prochaine fois on cherche depuis le début
        
  return(larow) ;
}

// Search in ListView
void Button2Click(object sender, EventArgs e)
 {
    label8.Visible = false ; // Done ...
    
    if (textBox2.Text == "")
        return ;
    if (textBox2.Text.Replace(" ", "") == "")
        return ;
        
// On what listview (what database) we are ?
   ListView lv ;
   int idbid = -1 ;
   
    switch (TabControl1.SelectedIndex) {
        case 0:
            idbid = IPD_GeneralLib.IPDGeneral.DBAbsoluteIdentifier.absolute_idMemos ;
            lv = listView1 ;
            break;
        case 1:
            idbid = IPD_GeneralLib.IPDGeneral.DBAbsoluteIdentifier.absolute_idPhone_Call_Log ;
            lv = listView2 ;
            break;
        case 2:
            idbid = IPD_GeneralLib.IPDGeneral.DBAbsoluteIdentifier.absolute_idTasks ;
            lv = listView3 ;
            break;
        case 3:
            idbid = IPD_GeneralLib.IPDGeneral.DBAbsoluteIdentifier.absolute_idCalendar ;
            lv = listView4 ;
            break;
        case 4:
            idbid = IPD_GeneralLib.IPDGeneral.DBAbsoluteIdentifier.absolute_idContacts ;
            lv = listView5 ;
            break;
        case 5:
            idbid = IPD_GeneralLib.IPDGeneral.DBAbsoluteIdentifier.absolute_idSMS ;
            lv = listView6 ;
            break;
        default:
            // Unknown, nothing to do
            idbid = -1 ;
            lv = null ;
        	break;
    }
 
   int inbrow = lv.Items.Count ;
   
   if ((idbid != -1) && (inbrow != 0) && (lv != null)) // Known DB
      {
       int ii = searchwordinlistview(lv, textBox2.Text) ;
       if (ii != -1)
          {
           // These 3 lines to ensure the found row is not on the last line in listview
           // Except if this row is the last row in listview
           int lviindex = ii + 3 ;
           if ((lviindex < 0) || (lviindex > inbrow-1) || (lv.TopItem.Index > lviindex))
               lviindex = ii ;
           lv.Items[lviindex].EnsureVisible() ;
           COMPUTEselect = true ;
           lv.Items[ii].Selected = true ;
           COMPUTEselect = false ;
        }
     }
}

void init_colnames_forContacts()
 {
  listView5.Columns[0].Name = "rec #" ;
  // For Contacts, the last column is not displayed inListview because it contains
  // (if any) a photo of this contact
  for (int ii = 0 ; ii < IPD_GeneralLib.IPDGeneral.sContact_Col_Names.Length-1 ; ii++)
      listView5.Columns[ii+1].Text = IPD_GeneralLib.IPDGeneral.sContact_Col_Names[ii] ;

}

void init_colnames_forTasks()
 {
  listView3.Columns[0].Name = "rec #" ;
  for (int ii = 0 ; ii < IPD_GeneralLib.IPDGeneral.sTask_Col_Names.Length ; ii++)
      listView3.Columns[ii+1].Text = IPD_GeneralLib.IPDGeneral.sTask_Col_Names[ii] ;

}

void init_colnames_forPhonecallLog()
 {
  listView2.Columns[0].Name = "rec #" ;
  for (int ii = 0 ; ii < IPD_GeneralLib.IPDGeneral.sPhoneCallLog_Col_Names.Length ; ii++)
      listView2.Columns[ii+1].Text = IPD_GeneralLib.IPDGeneral.sPhoneCallLog_Col_Names[ii] ;

}

void init_colnames_forMemos()
 {
  listView1.Columns[0].Name = "rec #" ;
  for (int ii = 0 ; ii < IPD_GeneralLib.IPDGeneral.sMemo_Col_Names.Length ; ii++)
      listView1.Columns[ii+1].Text = IPD_GeneralLib.IPDGeneral.sMemo_Col_Names[ii] ;

}

void init_colnames_forCalendar()
 {
  listView4.Columns[0].Name = "rec #" ;
  for (int ii = 0 ; ii < IPD_GeneralLib.IPDGeneral.sCalendar_Col_Names.Length ; ii++)
      listView4.Columns[ii+1].Text = IPD_GeneralLib.IPDGeneral.sCalendar_Col_Names[ii] ;

}

void init_colnames_forSms()
 {
  listView6.Columns[0].Name = "rec #" ;
  for (int ii = 0 ; ii < IPD_GeneralLib.IPDGeneral.sSms_Col_Names.Length ; ii++)
      listView6.Columns[ii+1].Text = IPD_GeneralLib.IPDGeneral.sSms_Col_Names[ii] ;

}

/*
void init_colnames_forMessages()
 {
  listView7.Columns[0].Name = "rec #" ;
  for (int ii = 0 ; ii < IPD_GeneralLib.IPDGeneral.sMessages_Col_Names.Length ; ii++)
      listView7.Columns[ii+1].Text = IPD_GeneralLib.IPDGeneral.sMessages_Col_Names[ii] ;

}
*/
        
void TabControl1Selected(object sender, TabControlEventArgs e)
 {
    label8.Visible = false ; // Done ...
    
    if (TabControl1.SelectedIndex == 0)
       button5.Text = "Memos to csv" ;

    if (TabControl1.SelectedIndex == 1)
       button5.Text = "P. calllog to csv" ;

    if (TabControl1.SelectedIndex == 2)
       button5.Text = "Tasks to csv" ;

    if (TabControl1.SelectedIndex == 3)
      {
       button4.Visible = true ; // enable export vcal
       button5.Text = "Calendar to csv" ;
    }
      else
           {
            button4.Visible = false ; // disable export vcal
         }

    if (TabControl1.SelectedIndex == 4)
      {
       button3.Visible = true ; // enable export vcard
       button5.Text = "Contact to csv" ;
    }
      else
           {
            button3.Visible = false ; // disable export vcard
         }

    if (TabControl1.SelectedIndex == 5)
       button5.Text = "Sms to csv" ;

}

void ListView1SelectedIndexChanged(object sender, EventArgs e)
 {
   if (COMPUTEselect == true)
     return ;
    
   ListView lv = (ListView) sender ;
   
   if (lv.SelectedIndices.Count != 0) // because before new row is selected, old row is unselected
       LASTROWSEARCH = lv.SelectedIndices[0] ;
           
}

// Select a File
void Button6Click(object sender, EventArgs e)
{
    label8.Visible = false ; // Done ...
    
    string fname = "" ;
	
	openFileDialog1.Filter = "BlackBerry Databases (*.ipd)|*.ipd|" +
		                     "All Files (*.*)|*.*" ;
	openFileDialog1.FileName = "" ;
	
	if (MainForm.ARGVDIR != "")
	   openFileDialog1.InitialDirectory = MainForm.ARGVDIR ;
	
	if (openFileDialog1.ShowDialog()!= DialogResult.OK)
        return ;
		
	ARGVFNAME = openFileDialog1.FileName;
	
	if (MainForm.ARGVDIR != "")
	   {
        System.IO.FileInfo f = new System.IO.FileInfo(fname) ;
	    MainForm.ARGVDIR = f.DirectoryName ;
	 }
	
	openFileDialog1.Dispose();
	
	Clean_all_formobjects();
    label8.Text = "Please, wait ..." ;
    label8.Visible = true ;
	this.Refresh() ;
	
    Parse_new_IpdFile() ;
    
    label8.Visible = false ;
    label8.Text = "Done ..." ;
}

void Clean_all_formobjects()
 {
    listView1.Items.Clear();
    listView2.Items.Clear();
    listView3.Items.Clear();
    listView4.Items.Clear();
    listView5.Items.Clear();
    listView6.Items.Clear();
    listBox1.Items.Clear();
    label4.Text = "";
    label6.Text = "";
    label9.Text = "";
    TabControl1.TabPages[0].Text = "Memos" + " (" + 0 + ")" ;
    TabControl1.TabPages[1].Text = "Phone CallLog" + " (" + 0 + ")" ;
    TabControl1.TabPages[2].Text = "Tasks" + " (" + 0 + ")" ;
    TabControl1.TabPages[3].Text = "Calendar" + " (" + 0 + ")" ;
    TabControl1.TabPages[4].Text = "Contacts" + " (" + 0 + ")" ;
    TabControl1.TabPages[5].Text = "Sms" + " (" + 0 + ")" ;

 
}

void Button7Click(object sender, EventArgs e)
 {
   Csv_Choose_and_resolve mafenetre1 = new Csv_Choose_and_resolve() ; // pour pouvoir ouvrir une form !!!!
   mafenetre1.ShowDialog() ;
}

    }
}
